﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenESSOConfigurator.PresentationLayer.PresentationModels;
using OpenESSOConfigurator.PresentationLayer.EventBroker;
using OpenESSOConfigurator.PresentationLayer.EventBroker.Handlers;
using OpenESSOConfigurator.BusinessLayer.Atoms;

namespace OpenESSOConfigurator.PresentationLayer.UserControls
{
    public partial class PassworPolicyConfig : UserControl,IView
    {
        
        public PassworPolicyConfig()
        {
            InitializeComponent();
            this.lenghtBindingSource.DataSource = PasswordPolicyPresentationModel.Instance.PasswordPolicy.Lenght;
            this.upperCaseBindingSource.DataSource = PasswordPolicyPresentationModel.Instance.PasswordPolicy.UpperCase;
            this.lowerCaseBindingSource.DataSource = PasswordPolicyPresentationModel.Instance.PasswordPolicy.LowerCase;
            this.numericBindingSource.DataSource = PasswordPolicyPresentationModel.Instance.PasswordPolicy.Numeric;
            this.specialCharBindingSource.DataSource = PasswordPolicyPresentationModel.Instance.PasswordPolicy.SpecialChar;
            this.previousPasswordBindingSource.DataSource = PasswordPolicyPresentationModel.Instance.PasswordPolicy.PreviousPassword;
            this.sequenceBindingSource.DataSource = PasswordPolicyPresentationModel.Instance.PasswordPolicy.Sequence;
            this.dictionaryBindingSource.DataSource = PasswordPolicyPresentationModel.Instance.PasswordPolicy.Dictionary;
            this.PasswordPolicyBindingSource.DataSource = PasswordPolicyPresentationModel.Instance.PasswordPolicy;
        }

        private void PasswordPolicy_Load(object sender, EventArgs e)
        {
            PasswordPolicyPresentationModel.Instance.RegisterView(this);
            this.Update();
        }
        public new void Update()
        {

        }

        #region Events Handlers
        private void Checkbox3_CheckedChanged(object sender, EventArgs e)
        {
            
            if (this.checkBox3.Checked)
                this.groupBox3.Enabled = true;
            else
                this.groupBox3.Enabled = false;
        }
        private void checkBox10_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox10.Checked)
                this.groupBox5.Enabled = true;
            else
                this.groupBox5.Enabled = false;
        }
        private void CheckBox14_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox14.Checked)
                this.groupBox6.Enabled = true;
            else
                this.groupBox6.Enabled = false;
        }
        private void CheckBox18_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox18.Checked)
                this.groupBox7.Enabled = true;
            else
                this.groupBox7.Enabled = false;
        }
        private void CheckBox26_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox26.Checked)
                this.groupBox8.Enabled = true;
            else
                this.groupBox8.Enabled = false;
        }
        private void CheckBox23_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox23.Checked)
                this.groupBox9.Enabled = true;
            else
                this.groupBox9.Enabled = false;
        }
        private void CheckBox28_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox28.Checked)
                this.groupBox10.Enabled = true;
            else
                this.groupBox10.Enabled = false;
        }
        private void CheckedBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox1.Checked)
                this.numericUpDown1.Enabled = true;
            else
                this.numericUpDown1.Enabled = false;
        }
        private void CheckBox2_CheckedBoxChanged(object sender, EventArgs e)
        {
            if (this.checkBox2.Checked)
                this.numericUpDown2.Enabled = true;
            else
                this.numericUpDown2.Enabled = false;
        }
        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            this.numericUpDown2.Minimum = this.numericUpDown1.Value;
        }
        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {
            this.numericUpDown1.Maximum = this.numericUpDown2.Value;
        }
        private void MustStart_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox mustStartcheckbox = (CheckBox)sender;
            if (mustStartcheckbox.Checked)
            {
                this.checkBox4.Enabled = false;
                this.checkBox9.Enabled = false;
                this.checkBox13.Enabled = false;
                this.checkBox17.Enabled = false;
                mustStartcheckbox.Enabled = true;
            }
            else
            {
                this.checkBox4.Enabled = true;
                this.checkBox9.Enabled = true;
                this.checkBox13.Enabled = true;
                this.checkBox17.Enabled = true;
            }
        }
        private void CheckBox21_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox21.Checked)
            {
                this.numericUpDown12.Enabled = true;
                if (this.checkBox2.Checked)
                    this.numericUpDown12.Maximum = this.numericUpDown2.Value;
                else this.numericUpDown12.Maximum = 200;
            }
            else
                this.numericUpDown12.Enabled = false;  
        }
        private void CheckBox5_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox5.Checked)
                this.numericUpDown3.Enabled = true;
            else
                this.numericUpDown3.Enabled = false;
        }
        private void CheckBox6_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox6.Checked)
                this.numericUpDown4.Enabled = true;
            else
                this.numericUpDown4.Enabled = false;
        }
        private void CheckBox8_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox8.Checked)
                this.numericUpDown6.Enabled = true;
            else
                this.numericUpDown6.Enabled = false;
        }
        private void CheckBox7_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox7.Checked)
                this.numericUpDown5.Enabled = true;
            else
                this.numericUpDown5.Enabled = false;
        }
        private void CheckBox12_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox12.Checked)
                this.numericUpDown8.Enabled = true;
            else
                this.numericUpDown8.Enabled = false;
        }
        private void CheckBox11_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox11.Checked)
                this.numericUpDown7.Enabled = true;
            else
                this.numericUpDown7.Enabled = false;
        }
        private void CheckBox16_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox16.Checked)
                this.numericUpDown10.Enabled = true;
            else
                this.numericUpDown10.Enabled = false;
        }
        private void CheckBox15_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox15.Checked)
                this.numericUpDown9.Enabled = true;
            else
                this.numericUpDown9.Enabled = false;
        }
        private void CheckBox19_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox19.Checked)
                this.textBox5.Enabled = true;
            else
                this.textBox5.Enabled = false;
        }
        private void CheckBox20_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox20.Checked)
                this.textBox6.Enabled = true;
            else
                this.textBox6.Enabled = false;
        }

        private void TextBox4_TextChanged(object sender, EventArgs e)
        {
            String pwdToTest = this.textBox4.Text;
            Password password=new Password(pwdToTest,PasswordPolicyPresentationModel.Instance.PasswordPolicy);
            if (pwdToTest.Length > 0)
            {
                if (!password.IsPasswordRespectPolicy())
                {
                    this.textBox4.BackColor = Color.Orange;
                }
                else
                {
                    this.textBox4.BackColor = Color.GreenYellow;
                }
            }
            else
                this.textBox4.BackColor = Color.White;
        }
        #endregion

        // TODO: completer la méthode de génération des mot de passe limite 20.
        private void button2_Click(object sender, EventArgs e)
        {
            //this.richTextBox2.Text=
            
        }

        private void expandButton_Clicked(object sender, EventArgs e)
        {
            ToolStripButton toolStripButton = (ToolStripButton)sender;
            if (toolStripButton.Tag.Equals("-"))
            {
                toolStripButton.Tag = "+";
                toolStripButton.Image = global::OpenESSOConfigurator.Properties.Resources.UnExpand;
                ToolStrip tls = (ToolStrip)toolStripButton.Owner;
                ToolStripContainer tlscon = (ToolStripContainer)tls.Parent.Parent;
                tlscon.Tag = tlscon.Height;
                tlscon.Height=tlscon.TopToolStripPanel.Height;
            }
            else
            {
                toolStripButton.Tag = "-";
                toolStripButton.Image = global::OpenESSOConfigurator.Properties.Resources.Expand;
                ToolStrip tls = (ToolStrip)toolStripButton.Owner;
                ToolStripContainer tlscon = (ToolStripContainer)tls.Parent.Parent;
                tlscon.Height =Convert.ToInt32(tlscon.Tag.ToString());
            }
        }

    }
}
